% input:  kinematics_roi.csv
% calculate  kinematics: mean ?????? std
% output: kinematics.mat 
clc;
num_choose = {'05', '06', '07', '08', '09', '10', '11', '16','17','18','19','20','22','23','25','26'};
fes_group = {'05', '06','09','10','11','19','20','25','26'};
%num_choose = {'05','06'};
eva_choose = {'pre','post'};
task_choose = {'FR','LR'};

fs = 1000;
% choose data path & save path
[subject_folder,kinematics_save,sep_typle] = file_path();

for si = 1:1:length(num_choose)
    subject_num = num_choose(si);
    
    for eva_i = 1:1:length(eva_choose)
        eva = eva_choose(eva_i);
        for task_i = 1:1:length(task_choose)
            task = task_choose(task_i);
            normal_subject = strcat('kinematics_c01_',lower(task),'.mat');
            normal_gaussian = strcat('kinematics_c01_',lower(task),'_gaussian.mat');
            load(normal_subject{1,1});
            load(normal_gaussian{1,1});
            % subject data path:
            subject_data = strcat(subject_folder,'s',subject_num,'_',eva,sep_typle,task,sep_typle);
            Files = dir(subject_data{1,1});
            LengthFiles = length(Files);   
            % preprocess kinematics
            for j = 3:LengthFiles
                % load kinematics_roi data
               [kinematics_roi] = load_raw_data(subject_data,Files,j);
               % calculate kinematics
               [kinematics_each_trial(j-2)] = calculate_kine_results_roi(kinematics_roi,normal_kinematics,gaussian,task,fs);
            end
            
            %% all trials
%             [kinematics_package_temp] = kinematics_package_each_trial(kinematics_each_trial,LengthFiles-2,subject_num,task,eva,fes_group);
%             if(strcmp(subject_num{1,1},'05') && strcmp(eva{1,1},'pre') && strcmp(task{1,1},'FR'))
%                 
%                 kinematics_package_alltrials = kinematics_package_temp;
%             else
%                 kinematics_package_alltrials = [kinematics_package_alltrials,kinematics_package_temp];
%             end   
%             
           %% mean + std
            %calculate mean & std:
            [kinematics_final] = kinematics_mean_std(kinematics_each_trial);
            
             %save kinematics.mat 
             save_path = strcat(kinematics_save,'kinematics_s',subject_num,'_',eva,'_',lower(task),'.mat');
             save(save_path{1,1},'kinematics_final');  
             
             clear kinematics_each_trial
             clear normal_kinematics
             clear kinematics_package_temp
        end          
    end
end

%csv_path = strcat(kinematics_save,'fes_kinematics_trials_20200212.csv');
%writetable(struct2table(kinematics_package_alltrials),csv_path);



